﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThietBiSo.LinQDB;

namespace ThietBiSo.Models
{
    public class MOnline
    {
        private _24hmuaonlineDataContext context = new _24hmuaonlineDataContext();
        MLoaiOnline mlonl = new MLoaiOnline();
        public List<Online> FindAll()
        {
            var v = from f in context.Onlines orderby f.ThuTu descending select f;
            return v.ToList();
        }
        public List<Online> FindAll_Web()
        {
            var v = from f in context.Onlines.Where(d=>d.HienThi==true) orderby f.ThuTu descending select f;
            return v.ToList();
        }
        public int GetTotalOnline()
        {
            return FindAll_Web().Count();
        }
        public void Save(Online onl)
        {
            context.Onlines.InsertOnSubmit(onl);
            context.SubmitChanges();
        }
        public void Update(Online onl)
        {
            var v = from f in context.Onlines
                    select f;
            Online onldb = v.SingleOrDefault(d => d.MaOnline.Equals(onl.MaOnline));
            onldb.TieuDe = onl.TieuDe;
            onldb.MaLoaiOnline = onl.MaLoaiOnline;
            onldb.TenOnline = onl.TenOnline;
            onldb.ThuTu = onl.ThuTu;
            context.SubmitChanges();
        }
        public void HienThi(int id)
        {
            var v = from f in context.Onlines where f.MaOnline==id
                    select f;
            Online spdb = v.SingleOrDefault(d => d.MaOnline==id);
            if (spdb.HienThi == true)
            {
                spdb.HienThi = false;
            }
            else spdb.HienThi = true;
            context.SubmitChanges();
        }
        public void Delete(int id)
        {
            var v = from p in context.Onlines
                    where p.MaOnline.Equals(id)
                    select p;
            Online onldb = v.SingleOrDefault();
            context.Onlines.DeleteOnSubmit(onldb);
            context.SubmitChanges();
        }
        public Online GetByID(int id)
        {
            return (FindAll().Where(d => d.MaOnline == id)).SingleOrDefault();
        }
        public int GetMax()
        {
            return (from t in context.Onlines
                    orderby t.MaOnline descending
                    select t.MaOnline).FirstOrDefault();
        }
        public int? GetMaxTT()
        {
            return (from t in context.Onlines
                    orderby t.ThuTu descending
                    select t.ThuTu).FirstOrDefault();
        }

    }
}